<? /* license

BilugCMS (http://www.bilug.it) - Content Management System for dynamic web sites
Copyright (C) 2005-2008  Federico Villa and Alessio Loro Piana

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

For reference, contact bilugcms@vilnet.it


license */ ?>
<?
require_once("auth.php");
require_once("../utility/connessione.php");
require_once("../utility/funzioni.php");
include_once("../utility/versione.php");

$soggetto = apici($_POST['soggetto']);
$testo = apici($_POST['testo']);
if (strlen($testo)<=6)
{
	$tipoerr="ERRORE: NON HAI SCRITTO NULLA";
	//echo "ERRORE: NON HAI SCRITTO NULLA";
   //echo "<br/><a href=\"area.php?pag=insert_newsletter.php\">Riprova</a>";
   echo "<meta http-equiv=\"refresh\" content=\"0;url=area.php?pag=insert_newsletter.php&errore=si&tipoerr=$tipoerr&soggetto=$soggetto&testo=$testo\" />";
}
else
{
    	$str="SELECT email FROM newsletter where stato=0";
    	// query email
    	$risultato=mysql_query($str);
    	if (mysql_num_rows($risultato)>0)
    	{
    		$mail ="";
    		while($control=mysql_fetch_row($risultato))
    		{
    			if ($mail != "") $mail .= ",";
    			$mail .= $control[0]; 		
    		}
    		// recupero informazioni inserite
    		$body = stripslashes($testo);
  			$body = str_ireplace("src=\"/html/","src=\"http://".$_SERVER['SERVER_NAME']."/html/",$body);
    		
    		//preparazione struttura corpo email
    		
    		//lettura dell'intestazione base dell'email
    		$filename = "../custom/templatemail.html";
			$handle = fopen($filename, "r");
			$corpoemail = fread($handle, filesize($filename));
			fclose($handle);			
			$corpoemail = str_replace("src=\"..", "src=\"http://".$_SERVER['SERVER_NAME'],$corpoemail);
			if (strpos($corpoemail,"<versione>")!== false)
    		{
    			$corpoemail = str_replace("<versione>",$versione,$corpoemail);
			}    		
    		// lettura del logo personalizzato
    		if (strpos($corpoemail,"<logo>")!== false)
    		{
    			$filename = "../custom/logo.html";
				$handle = fopen($filename, "r");
				$contenuto = fread($handle, filesize($filename));
				fclose($handle);
				$contenuto = substr($contenuto,strpos($contenuto,"?>")+2);
				$contenuto = str_replace("src=\"..", "src=\"http://".$_SERVER['SERVER_NAME'],$contenuto);
				$contenuto = str_replace("index.php", "http://".$_SERVER['SERVER_NAME'],$contenuto);
    			$corpoemail = str_replace("<logo>",$contenuto,$corpoemail);
   		}
   		// lettura del piede personalizzato
   		if (strpos($corpoemail,"<piede>")!== false)
    		{
	    		$filename = "../custom/piede.html";
				$handle = fopen($filename, "r");
				$contenuto = fread($handle, filesize($filename));
				fclose($handle);
				$contenuto = substr($contenuto,strpos($contenuto,"?>")+2);
				$contenuto = str_replace("src=\"..", "src=\"http://".$_SERVER['SERVER_NAME'],$contenuto);
	    		$corpoemail = str_replace("<piede>",$contenuto,$corpoemail);
    		}
    		
    		$corpoemail = str_replace("<corpo>",$body,$corpoemail);
			
			$body = $corpoemail;
			
			//header email			
			$header = "From: webmaster@".substr($_SERVER['SERVER_NAME'],4)." \r\n";
			$header .= "bcc: ".$mail." \r\n";						
			$header .="X-Mailer : BiLugcms PHP/" . phpversion()."\r\n";
			$header .="MIME-Version: 1.0\r\n";
			$header .="Content-type: text/html; charset=iso-8859-15\r\n";
			$header .="\r\n";
			
			if ($soggetto == "") $soggetto = "Email dalla Newsletter di ".$_SERVER['SERVER_NAME'];
			$mail= "NewsLetter:;"; 
			if (mail( $mail, $soggetto, $body, $header))
					//Header("Location: ");
					echo "<meta http-equiv=\"refresh\" content=\"0;url=area.php?pag=conferma.php&from=insert_newsletter.php\" />";
			else 
			{
					$tipoerr="ERRORE: EMAIL NON INVIATE";
					//echo "ERRORE: EMAIL NON INVIATE";
	            //echo "<br/><a href=\"area.php?pag=insert_newsletter.php\">Riprova</a>";
	            echo "<meta http-equiv=\"refresh\" content=\"0;url=area.php?pag=insert_newsletter.php&errore=si&tipoerr=$tipoerr&soggetto=$soggetto&testo=$testo\" />";
	  		}
	  	}
	  	else	  	
			$tipoerr="ERRORE: NESSUN INSCRITTO ALLA NEWSLETTER";
			//echo "ERRORE: NESSUN INSCRITTO ALLA NEWSLETTER";
}
?>
